<?php
namespace Admin\Controller;

use Think\Controller;

class PositionController extends Controller
{
    public function index()
    {
        $positions = D('Position')->getPositions();
        $this->assign('positions' , $positions);
        $this->display();
    }

    public function add()
    {
        $url = '/admin.php?c=Position';
        if (IS_POST){
            $post['name'] = I('name');
            $post['status'] = I('status');
            $post['create_time'] = date('Y-m-d H:i' , time());
            $position = D('Position')->add($post);
            if ($position){
                return show(1, '添加成功' , $url);
            }else{
                return show(0, '新增失败' , $url);
            }

        }else{
            $this->display();
        }
    }

    public function setStatus($id , $status)
    {
        if (IS_POST){
            $data['id'] = $id;
            $data['status'] = $status;
            $url = $_SERVER['HTTP_REFERER'];
            $positionId = D('Position')->save($data);
            if ($positionId){
                return show(1 , '修改成功');
            }else{
                return show(0, '修改失败' , $url);
            }
        }
    }

    public function edit()
    {
        dump(I('id'));
        if (IS_POST){
            $positionRes = D('Position')->find(I('id'));
            $positionRes->name = I('name');
            $positionRes->description = I('description');
            $positionRes->status = I('status');
            $positionId = D('Position')->where('id=' . I('id'))->save();
            if($positionId){
                return show(1, '修改成功');
            }else{
                return show(0, '修改失败');
            }
        }
        $this->display();
    }
}